package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.PlugsTypeErr;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.PlugsTypeBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.result.PlugsTypeListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.result.PlugsTypeListResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.result.PlugsTypeResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.vo.PlugsTypeForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.plugs.vo.PlugsTypeListForm;
import com.cloudbroker.bcs.platform.dav.middle.entity.PlugsType;
import com.cloudbroker.bcs.platform.dav.middle.service.PlugsTypeService;

@Service
public class PlugsTypeBizServiceImpl implements PlugsTypeBizService{

	@Resource
	private PlugsTypeService plugsTypeService;
	
	@Override
	public HSBCPSBackVO funcPlugstypeSave(PlugsTypeForm form) {
		PlugsTypeResult result = new PlugsTypeResult();
		
		PlugsType existDb = new PlugsType();
		existDb.setName(form.getName());
		int count = plugsTypeService.count(existDb);
		if(count > 0){
			return VOUtil.genErrorResult(PlugsTypeErr.PLUGS_TYPE_EXIST, PlugsTypeResult.class);
		}
		
		PlugsType saveDb = new PlugsType();
		saveDb.setIcon(form.getIcon());
		saveDb.setName(form.getName());
		saveDb.setSort(Long.valueOf(form.getSort()));
		saveDb.setParent_id(StringUtil.isEmpty(form.getParent_id()) ? 0L : Long.parseLong(form.getParent_id()));
		saveDb.setLink(form.getLink());
		saveDb.setPlugs_type_status(Integer.valueOf(form.getPlugs_type_status()));
		plugsTypeService.save(saveDb);
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public HSBCPSBackVO funcPlugstypeUpdate(PlugsTypeForm form) {
		PlugsTypeResult result = new PlugsTypeResult();
		PlugsType existDb = new PlugsType();
		existDb.setName(form.getName());
		List<PlugsType> list = plugsTypeService.query(existDb);
		Long plugs_type_id = Long.valueOf(form.getPlugs_type_id());
		if(list != null){
			if(list.size() > 1){
				return VOUtil.genErrorResult(PlugsTypeErr.PLUGS_TYPE_EXIST, PlugsTypeResult.class);
			} else if(list.size() > 0){
				if(!plugs_type_id.equals(list.get(0).getPlugs_type_id())){
					return VOUtil.genErrorResult(PlugsTypeErr.PLUGS_TYPE_EXIST, PlugsTypeResult.class);
				}
			}
		}
		
		PlugsType updateDb = new PlugsType();
		updateDb.setPlugs_type_id(plugs_type_id);
		updateDb.setIcon(form.getIcon());
		updateDb.setName(form.getName());
		updateDb.setSort(Long.valueOf(form.getSort()));
		updateDb.setParent_id(StringUtil.isEmpty(form.getParent_id()) ? 0L : Long.parseLong(form.getParent_id()));
		updateDb.setLink(form.getLink());
		updateDb.setPlugs_type_status(Integer.valueOf(form.getPlugs_type_status()));
		plugsTypeService.update(updateDb);
		return VOUtil.setSuccessResult(result);
	}

	@Override
	public PlugsTypeListResult funcPlugstypeList(PlugsTypeListForm form) {
		PlugsTypeListResult result = new PlugsTypeListResult();
		PlugsType listDb = new PlugsType();
		
		listDb.setParent_id(null == form.getParent_id() || "".equals(form.getParent_id()) ? 0L : Long.parseLong(form.getParent_id()));
		List<PlugsType> list = plugsTypeService.query(listDb);
		if(list != null && list.size() > 0){
			List<PlugsTypeListData> tmpList = new ArrayList<PlugsTypeListData>();
			for (PlugsType plugsType : list) {
				PlugsTypeListData d = new PlugsTypeListData();
				BeanUtils.copyProperties(plugsType, d);
				tmpList.add(d);
			}
			result.newDataList().addAll(tmpList);
		}
		return VOUtil.setSuccessResult(result);
	}

}
